package cs271.examples.recursion;

/**
 * Here are some examples that illustrate mutual recursion.  Surely this
 * isn't the best way to solve the problem in this case, though it does
 * provide an interesting example.  Notice that both functions are
 * also tail-recursive, so in a language with proper tail-recursion, this
 * technique would incur no additional space requirements (however it still
 * scales with the size of n).
 *
 * This example is take from "On Lisp" by Paul Graham.
 */
public class MutualRecursion {
	
	public static boolean isEven(int n) {
		if(n==0)
			return true;
		else
			return isOdd(n-1);
	}
	
	public static boolean isOdd(int n) {
		if(n==0)
			return false;
		else 
			return isEven(n-1);
	}
	
	public static void main(String[] args) {
		System.out.println("5 is even: " + isEven(5));
		System.out.println("5 is odd: " + isOdd(5));
		System.out.println("6 is even: " + isEven(6));
		System.out.println("6 is odd: " + isOdd(6));
	}
}
